﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace MethodWorx.CMS.Plugins.AssetManagement
{
    [Serializable]
    public class AssetMap
    {
        public AssetMap()
        {
            Map = new Dictionary<string, Guid>();
        }

        [XmlArray("Map")]  
        [XmlArrayItem("MapEntry", Type=typeof(DictionaryEntry))]  
        public DictionaryEntry[] MapForSerialization
        {
            get  
             {  
                 //Make an array of DictionaryEntries to return  
                 DictionaryEntry[] ret=new DictionaryEntry[Map.Count];  
                 int i=0;  
                 DictionaryEntry de;  
                 //Iterate through Stuff to load items into the array.  
                 foreach (KeyValuePair<string, Guid> stuffLine in Map)  
                 {  
                     de = new DictionaryEntry();  
                     de.Key = stuffLine.Key;  
                     de.Value = stuffLine.Value;  
                     ret[i]=de;  
                     i++;  
                 }  
                 return ret;  
             }  
             set  
             {
                 Map.Clear();  
                 for (int i=0; i<value.Length; i++)  
                 {
                     Map.Add((string)value[i].Key, (Guid)value[i].Value);  
                 }  
             }  
        }


        [XmlIgnore]
        public Dictionary<string, Guid> Map
        {
            get;
            set;
        }

        public void RemoveByKey(Guid guid)
        {
            if (Map.Where(kvp => kvp.Value == guid).Any())
            {
                var found = Map.FirstOrDefault(kvp => kvp.Value == guid);
                Map.Remove(found.Key);
            }
        }
    }
}
